<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class M_teachers extends CI_Model {

    private $tmp = '';

    function __construct() {
        parent::__construct();
    }

    public function get_teachers() {
        $query = $this->db->get('teachers');
        return $query->result_array();
    }

    public function get_one_teacher($id = 0) {
        $query = $this->db->query('SELECT * FROM teachers WHERE id = "' . $id . '" ');
        return $query->result_array();
    }

    public function insert_teachers($teachers = array()) {

        $teachers = preg_split("/[,]/", $teachers);
        $teachers = array_diff($teachers, array(''));
        $teachers = array_map('trim', $teachers);
        $teachers = array_unique($teachers);

        foreach ($teachers as $names) {
            $query = $this->db->query('SELECT name FROM teachers WHERE name = "' . $names . '" ');
            if ($query->num_rows() > 0) {
                $this->tmp .= $names . ',';
            }
        }

        if ($this->tmp == '') {
            foreach ($teachers as $names2) {
                $this->db->insert('teachers', array('name' => $names2));
            }
            return lang('successfull');
        } else {
            return sprintf(lang('admin_name_required'), $this->tmp);
        }
    }

    public function edit_teacher($id = 0, $login = '', $name = '', $login = '', $password = '', $phone = '') {
    	$query = $this->db->query('SELECT id,login FROM teachers WHERE login = "' . $login . '" ');
        if ($query->num_rows() > 0) {
        	$row = $query->row();
        	if ($row->id != $id) {
            	return sprintf(lang('admin_login_required'), $login);
            }
            else
            {
	        	$this->db->update('teachers', array(
	        	'name'=>$name,
	        	'login'=>$login,
	        	'password'=>$password,
	        	'phone'=>$phone), array('id' => $id));
	        	return lang('successfull');
            }
        }
        else
        {
        	$this->db->update('teachers', array(
	        	'name'=>$name,
	        	'login'=>$login,
	        	'password'=>$password,
	        	'phone'=>$phone), array('id' => $id));
        	return lang('successfull');
        }
    }

    public function delete_teacher($teachers = array()) {

        foreach ($teachers as $id) {
            $this->db->delete('teachers', array('id' => $id));
        }
    }

}

/* End of file m_teachers.php */
/* Location: ./application/models/m_teachers.php */